
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="姓名111:" prop="name">
          <el-input v-model="formData.name" :clearable="true"  placeholder="请输入姓名" />
       </el-form-item>
        <el-form-item label="头像:" prop="avatar">
          <el-input v-model="formData.avatar" :clearable="true"  placeholder="请输入头像" />
       </el-form-item>
        <el-form-item label="身份证号:" prop="idNumber">
          <el-input v-model="formData.idNumber" :clearable="true"  placeholder="请输入身份证号" />
       </el-form-item>
        <el-form-item label="户籍地址:" prop="address">
          <el-input v-model="formData.address" :clearable="true"  placeholder="请输入户籍地址" />
       </el-form-item>
        <el-form-item label="联系电话:" prop="phone">
          <el-input v-model="formData.phone" :clearable="true"  placeholder="请输入联系电话" />
       </el-form-item>
        <el-form-item label="年龄:" prop="age">
          <el-input v-model.number="formData.age" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="性别:" prop="gender"> 
          <el-input v-model="formData.gender" :clearable="true" placeholder="请输入性别"/>
        </el-form-item>
        <el-form-item label="滞留地:" prop="retention">
          <el-input v-model="formData.retention" :clearable="true"  placeholder="请输入滞留地" />
       </el-form-item>
        <el-form-item label="上报乡镇:" prop="townCode">
           <el-select v-model="formData.townCode" placeholder="请选择上报乡镇" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in townOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="户籍乡镇:" prop="liveTownCode">
           <el-select v-model="formData.liveTownCode" placeholder="请选择户籍乡镇" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in townOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="标签:" prop="tag">
          <el-input v-model="formData.tag" :clearable="true"  placeholder="请输入标签" />
       </el-form-item>
        <el-form-item label="云搜截图:" prop="search">
          <SelectImage v-model="formData.search" file-type="image" :type="'retention-person-detail-'+formData.ID"/>
       </el-form-item>
        <el-form-item label="出入境截图:" prop="entry">
          <SelectImage v-model="formData.entry" file-type="image" :type="'retention-person-detail-'+formData.ID"  />
       </el-form-item>
        <el-form-item label="联系人:" prop="contactName">
          <el-input v-model="formData.contactName" :clearable="true"  placeholder="请输入联系人" />
       </el-form-item>
        <el-form-item label="联系人电话:" prop="contactPhone">
          <el-input v-model="formData.contactPhone" :clearable="true"  placeholder="请输入联系人电话" />
       </el-form-item>
        <el-form-item label="状态 init初始化 search云搜 entry出入境 wait待上报 report已上报 drop不能上报:" prop="state">
           <el-select v-model="formData.state" placeholder="请选择状态 init初始化 search云搜 entry出入境 wait待上报 report已上报 drop不能上报" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in retention_stateOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="附件:" prop="attachments">
          <SelectFile v-model="formData.attachments" />
       </el-form-item>
        <el-form-item label="活跃时间:" prop="activateAt">
          <el-date-picker v-model="formData.activateAt" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item>
        <el-form-item label="提交时间:" prop="submitAt">
          <el-date-picker v-model="formData.submitAt" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item>
        <el-form-item label="上报时间:" prop="reportAt">
          <el-date-picker v-model="formData.reportAt" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createCasRetentionPersons,
  updateCasRetentionPersons,
  findCasRetentionPersons
} from '@/api/retention_person/casRetentionPersons'

defineOptions({
    name: 'CasRetentionPersonsForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'
// 文件选择组件
import SelectFile from '@/components/selectFile/selectFile.vue'


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const townOptions = ref([])
const retention_stateOptions = ref([])
const formData = ref({
            name: '',
            avatar: '',
            idNumber: '',
            address: '',
            phone: '',
            age: undefined,
            retention: '',
            townCode: '',
            liveTownCode: '',
            tag: '',
            search: '',
            entry: '',
            contactName: '',
            contactPhone: '',
            state: '',
            attachments: [],
            activateAt: new Date(),
            submitAt: new Date(),
            reportAt: new Date(),
        })
// 验证规则
const rule = reactive({
               name : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               idNumber : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               address : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               townCode : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               liveTownCode : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               state : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               submitAt : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findCasRetentionPersons({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    townOptions.value = await getDictFunc('town')
    retention_stateOptions.value = await getDictFunc('retention_state')
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createCasRetentionPersons(formData.value)
               break
             case 'update':
               res = await updateCasRetentionPersons(formData.value)
               break
             default:
               res = await createCasRetentionPersons(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
